package com.cdp.system.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdp.common.core.domain.entity.*;
import com.cdp.system.dto.DeformationPerformanceDTO;
import com.cdp.system.dto.ExportDeformationPerformanceExcelDTO;
import com.cdp.system.mapper.SysCementMapper;
import com.cdp.system.mapper.SysDeformationPerformanceMapper;
import com.cdp.system.mapper.SysMechanicalPerformanceMapper;
import com.cdp.system.service.*;
import com.cdp.system.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysDeformationPerformanceServiceImpl extends ServiceImpl<SysDeformationPerformanceMapper, SysDeformationPerformance> implements ISysDeformationPerformanceService {


    @Autowired
    private  SysDeformationPerformanceMapper sysDeformationPerformanceMapper;


    @Autowired
    private SysMechanicalPerformanceMapper sysMechanicalPerformanceMapper;

    @Autowired
    private SysCementMapper sysCementMapper;



    @Autowired
    private IsysRegionService isysRegionService;

    @Autowired
    private ISysFlyashService sysFlyashService;

    @Autowired
    private ISysSandFinenesService sysSandFinenesService;

    @Autowired
    private ISysSandTypeService sysSandTypeService;

    @Autowired
    private ISysSandService sysSandService;

    @Autowired
    private ISysStoneGrainService sysStoneGrainService;

    @Autowired
    private ISysFlyashTypeService sysFlyashTypeService;

    @Autowired
    private ISysFlyashFinenessService sysFlyashFinenessService;

    @Autowired
    private ISysWaterReducerTypeService sysWaterReducerTypeService;

    @Autowired
    private ISysCementService sysCementService;

    @Autowired
    private ISysStoneService sysStoneService;

    @Autowired
    private ISysWaterReducerService sysWaterReducerService;

    @Autowired
    private ISysStoneSizeService stoneSizeService;

    @Autowired
    private ISysSourceNameService sysSourceNameService;
    @Autowired
    private ISysEfficiencyMerchantNameService sysEfficiencyMerchantNameService;


    @Autowired
    private ISysDeformationPerformanceService sysDeformationPerformanceService;

    @Autowired
    private ISysEfficiencyAgentService sysEfficiencyAgent;

    @Autowired
    private ISysDurabilityPerformanceService sysDurabilityPerformanceService;

    @Autowired
    private ISysWaterService waterService;

    @Autowired
    private ISysMineralPowderService sysMineralPowderService;

    @Autowired
    private SysOtherAdmixturesService sysOtherAdmixturesService;


    @Autowired
    private ISysWaterModelService sysWaterModelService;

    @Autowired
    private ISysSwellingagentService sysSwellingagentService;
    /**
     * 查询变形性能
     * @return
     */
    @Override
    public List<SysDeformationPerformance> listDeformationPerformance() {
        List<SysDeformationPerformance> list = list();
        return list;
    }

    /**
     * 查询最大值 最小值
     * @return
     */

    @Override
    public Map<String, Object> queryDeformationPerformanceMaxMin() {

        return     sysDeformationPerformanceMapper.queryDeformationPerformanceMaxMin();
    }
    @Override
    public List<DeformationPerformanceVO> queryDeformationPerformance(DeformationPerformanceDTO dto) {

        // 调用 Mapper 查询数据
        List<DeformationPerformanceVO> results = sysDeformationPerformanceMapper.selectByCriteria(dto);

        // 预加载相关数据，减少循环中查询
        Map<Long, SysWater> waterMap = waterService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysWater -> sysWater.getWaterId().longValue(),
                        item -> item
                ));



        Map<Long, SysCement> cementMap = sysCementService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysCement -> sysCement.getCementId().longValue(),
                        item -> item
                ));

        Map<Long, SysStone> stoneMap = sysStoneService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysStone -> sysStone.getStoneId().longValue(),
                        item -> item
                ));

        Map<Long, SysSand> sandMap = sysSandService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysSand -> sysSand.getSandId().longValue(),
                        item -> item
                ));

        Map<Long, SysFlyash> flyashMap = sysFlyashService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysFlyash -> sysFlyash.getFlyashId().longValue(),
                        item -> item
                ));





        Map<Long, SysWaterReducer> reducerMap = sysWaterReducerService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysWaterReducer -> sysWaterReducer.getReducerId().longValue(),
                        item -> item
                ));

        Map<Long, SysEfficiencyAgent> efficiencyAgentMap = sysEfficiencyAgent.list()
                .stream()
                .collect(Collectors.toMap(
                        sysEfficiencyAgent -> sysEfficiencyAgent.getAgentId().longValue(),
                        item -> item
                ));

        Map<Long, SysMineralPowder> mineralPowderMap = sysMineralPowderService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysMineralPowder -> sysMineralPowder.getPowderId().longValue(),
                        item -> item
                ));

        Map<Long, SysOtherAdmixtures> otherAdmixturesMap = sysOtherAdmixturesService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysOtherAdmixtures -> sysOtherAdmixtures.getAdmixturesId().longValue(),
                        item -> item
                ));

        Map<Long, SysSwellingagent> swellingagentMap = sysSwellingagentService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysSwellingagent -> sysSwellingagent.getSwellingagentId().longValue(),
                        item -> item
                ));

        // 转换数据
        return results.stream().map(entity -> {
            DeformationPerformanceVO vo = new DeformationPerformanceVO();
            BeanUtils.copyProperties(entity, vo);


            // 设置水数据
            if (entity.getWaterId() != null) {
                SysWater sysWater = waterMap.get(Long.valueOf(entity.getWaterId()));
                if (sysWater != null) {
                    SysWaterVO sysWaterVO = new SysWaterVO();
                    BeanUtils.copyProperties(sysWater, sysWaterVO);
                    vo.setWaterModel(sysWater.getModel());
                    vo.setSysWaterVO(sysWaterVO);
                }
            }

            // 设置水泥数据
            if (entity.getCementId() != null) {
                SysCement sysCement = cementMap.get(Long.valueOf(entity.getCementId()));
                if (sysCement != null) {
                    SysCementVO sysCementVO = new SysCementVO();
                    BeanUtils.copyProperties(sysCement, sysCementVO);
                    vo.setSysCementVO(sysCementVO);
                    vo.setCementModel(sysCementVO.getModel());
                }
            }

            // 设置石头数据
            if (entity.getStoneId() != null) {
                SysStone sysStone = stoneMap.get(Long.valueOf(entity.getStoneId()));
                if (sysStone != null) {
                    SysStoneVO sysStoneVO = new SysStoneVO();
                    BeanUtils.copyProperties(sysStone, sysStoneVO);
                    vo.setSysStoneVO(sysStoneVO);
                    vo.setStoneModel(sysStoneVO.getModel());

                }
            }

            // 设置砂数据
            if (entity.getSandId() != null) {
                SysSand sysSand = sandMap.get(Long.valueOf(entity.getSandId()));
                if (sysSand != null) {
                    SysSandVO sysSandVO = new SysSandVO();
                    BeanUtils.copyProperties(sysSand, sysSandVO);
                    vo.setSysSandVO(sysSandVO);
                    vo.setSandModel(sysSandVO.getType());
                }
            }

            // 设置粉煤灰数据
            if (entity.getFlyAshId() != null) {
               // SysFlyash sysFlyash = flyashMap.get(Long.parseLong(entity.getFlyAshId());
                long flyAshId = entity.getFlyAshId().longValue();  // 将 Integer 转换为 long
                SysFlyash sysFlyash = flyashMap.get(flyAshId);  // 使用 long 类型的键来查找


                if (sysFlyash != null) {
                    SysFlyashVO sysFlyashVO = new SysFlyashVO();
                    BeanUtils.copyProperties(sysFlyash, sysFlyashVO);
                    vo.setSysFlyashVO(sysFlyashVO);
                    vo.setFlyModel(sysFlyashVO.getGrade());
                }
            }

            // 设置减水剂数据
            if (entity.getReducerId() != null) {
                SysWaterReducer sysWaterReducer = reducerMap.get(Long.valueOf(entity.getReducerId()));
                if (sysWaterReducer != null) {
                    AdditiveWaterReducerVO waterReducerVO = new AdditiveWaterReducerVO();
                    BeanUtils.copyProperties(sysWaterReducer, waterReducerVO);
                    vo.setAdditiveWaterReducerVO(waterReducerVO);
                    vo.setWaterReducerModel(waterReducerVO.getModel());

                }
            }

            // 设置增效剂数据
            if (entity.getAgentId() != null) {
                SysEfficiencyAgent sysEfficiencyAgent = efficiencyAgentMap.get(Long.valueOf(entity.getAgentId()));
                if (sysEfficiencyAgent != null) {
                    SysEfficiencyAgentVO sysEfficiencyAgentVO = new SysEfficiencyAgentVO();
                    BeanUtils.copyProperties(sysEfficiencyAgent, sysEfficiencyAgentVO);
                    vo.setSysEfficiencyAgentVO(sysEfficiencyAgentVO);
                    vo.setAgentModel(sysEfficiencyAgent.getModel());

                }
            }

            // 设置矿粉数据
            if (entity.getPowderId() != null) {
                SysMineralPowder sysMineralPowder = mineralPowderMap.get(Long.valueOf(Long.valueOf(entity.getPowderId())));
                if (sysMineralPowder != null) {
                    SysMineralPowderVO sysMineralPowderVO = new SysMineralPowderVO();
                    BeanUtils.copyProperties(sysMineralPowder, sysMineralPowderVO);
                    vo.setMineralPowderVO(sysMineralPowderVO);
                    vo.setMineralPowderModel(sysMineralPowderVO.getModel());
                }
            }

            // 设置其他数据
            if (entity.getAdmixturesId() != null) {
                // 将 String 类型的 admixturesId 转换为 long 类型
                String admixturesIdStr = entity.getAdmixturesId();
                long admixturesId = Long.parseLong(admixturesIdStr);  // 将字符串转换为 long
                // 修改为从 otherAdmixturesMap 获取 SysOtherAdmixtures 对象
                SysOtherAdmixtures sysOtherAdmixtures = otherAdmixturesMap.get(admixturesId);  // 使用 long 类型的键来查找

                System.out.println("Checking flyashMap for flyAshId哈哈哈: " + sysOtherAdmixtures);
                System.out.println("flyashMap:哈哈哈 你好" + admixturesId);  // 打印整个 map 内容
                if (sysOtherAdmixtures != null) {
                    SysOtherAdmixturesVO sysOtherAdmixturesVO = new SysOtherAdmixturesVO();
                    BeanUtils.copyProperties(sysOtherAdmixtures, sysOtherAdmixturesVO);
                    vo.setSysOtherAdmixturesVO(sysOtherAdmixturesVO);
                    vo.setOtherAdmixturesModel(sysOtherAdmixturesVO.getModel());
                }
            }

            // 设置膨胀剂数据
            if (entity.getSwellingagentId() != null) {
                SysSwellingagent sysSwellingagent = swellingagentMap.get(entity.getSwellingagentId().longValue());
                if (sysSwellingagent != null) {
                    SwellingagentVO swellingagentVO = new SwellingagentVO();
                    BeanUtils.copyProperties(sysSwellingagent, swellingagentVO);
                    vo.setSwellingagentVO(swellingagentVO);
                    vo.setSwellingagentModel(swellingagentVO.getModel());
                }
            }

            BigDecimal waterDosage = entity.getWaterDosage();
            BigDecimal powderDosage = entity.getPowderDosage();
            BigDecimal admixturesDosage = entity.getAdmixturesDosage();
            BigDecimal swellingagentDosage = entity.getSwellingagentDosage();
            System.out.println("水掺量"+waterDosage);
            System.out.println("矿粉掺量"+powderDosage);
            System.out.println("其他参合料掺量"+admixturesDosage);
            System.out.println("膨胀剂"+swellingagentDosage);

            return vo;
        }).collect(Collectors.toList());
    }



//    @Override
//    public List<DeformationPerformanceVO> queryDeformationPerformance(DeformationPerformanceDTO dto) {
//
//        // 调用 Mapper 查询数据
//        List<DeformationPerformanceVO> results = sysDeformationPerformanceMapper.selectByCriteria(dto);
//
//
//        // Step 2: 预加载相关数据，减少循环中查询，转换 Integer -> Long
//        Map<Long, SysWater> waterMap = waterService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysWater -> sysWater.getWaterId().longValue(),
//                        item-> item
//                ));
//
//
//        Map<Long, SysCement> cementMap = sysCementService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysCement -> sysCement.getCementId().longValue(),
//                        item -> item
//                ));
//
//        Map<Long, SysStone> stoneMap = sysStoneService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysStone -> sysStone.getStoneId().longValue(),
//                        item -> item
//                ));
//
//        Map<Long, SysSand> sandMap = sysSandService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysSand -> sysSand.getSandId().longValue(),
//                        item -> item
//                ));
//
//        Map<Long, SysFlyash> flyashMap = sysFlyashService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysFlyash -> sysFlyash.getFlyashId().longValue(),
//                        item -> item
//                ));
//
//        Map<Long, SysWaterReducer> reducerMap = sysWaterReducerService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysWaterReducer -> sysWaterReducer.getReducerId().longValue(),
//                        item -> item
//                ));
//
//        Map<Long, SysEfficiencyAgent> efficiencyAgentMap = sysEfficiencyAgent.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysEfficiencyAgent -> sysEfficiencyAgent.getAgentId().longValue(),
//                        item -> item
//                ));
//
//        Map<Long, SysMineralPowder> mineralPowderMap = sysMineralPowderService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysMineralPowder -> sysMineralPowder.getPowderId().longValue(),
//                        item -> item
//                ));
//
//        Map<Long, SysOtherAdmixtures> otherAdmixturesMap = sysOtherAdmixturesService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        sysOtherAdmixtures -> sysOtherAdmixtures.getAdmixturesId().longValue(),
//                        item -> item
//                ));
//
//        //膨胀剂
//        Map<Long, SysSwellingagent> swellingagentMap = sysSwellingagentService.list()
//                .stream()
//                .collect(Collectors.toMap(
//                        swellingagent -> swellingagent.getSwellingagentId().longValue(),
//                        item -> item
//                ));
//
//
//        results.stream().map(entity -> {
//            DeformationPerformanceVO vos = new DeformationPerformanceVO();
//            BeanUtils.copyProperties(entity, vos);
//
//            System.out.println("447"+entity.getWaterId());
//            // 设置水数据
//            if (entity.getWaterId() != null) {
//                SysWater sysWater = waterMap.get(entity.getWaterId());
//                if (sysWater != null) {
//                    SysWaterVO sysWaterVO = new SysWaterVO();
//                    BeanUtils.copyProperties(sysWater, sysWaterVO);
//                    vos.setWaterList(Collections.singletonList(sysWaterVO));
//                } else {
//                    vos.setWaterList(Collections.emptyList());
//                }
//            }
//
//
//
//            // 设置水泥数据
//            if (entity.getCementId() != null) {
//                SysCement sysCement = cementMap.get(entity.getCementId());
//                if (sysCement != null) {
//                    SysCementVO sysCementVO = new SysCementVO();
//                    BeanUtils.copyProperties(sysCement, sysCementVO);
//                    vos.setCementList(Collections.singletonList(sysCementVO));
//                } else {
//                    vos.setCementList(Collections.emptyList());
//                }
//            }
//
//            // 设置石头数据
//            if (entity.getStoneId() != null) {
//                SysStone sysStone = stoneMap.get(entity.getStoneId());
//                if (sysStone != null) {
//                    SysStoneVO sysStoneVO = new SysStoneVO();
//                    BeanUtils.copyProperties(sysStone, sysStoneVO);
//                    vos.setStoneList(Collections.singletonList(sysStoneVO));
//                } else {
//                    vos.setStoneList(Collections.emptyList());
//                }
//            }
//
//            // 设置砂数据
//            if (entity.getSandId() != null) {
//                SysSand sysSand = sandMap.get(entity.getSandId());
//                if (sysSand != null) {
//                    SysSandVO sysSandVO = new SysSandVO();
//                    BeanUtils.copyProperties(sysSand, sysSandVO);
//                    sysSandVO.setFineness(sysSand.getFineness());
//                    vos.setSandtList(Collections.singletonList(sysSandVO));
//                } else {
//                    vos.setSandtList(Collections.emptyList());
//                }
//            }
//
///*            // 设置粉煤灰数据
//            if (entity.getFlyashId() != null) {
//                SysFlyash sysFlyash = flyashMap.get(entity.getFlyashId());
//                if (sysFlyash != null) {
//                    SysFlyashVO sysFlyashVO = new SysFlyashVO();
//                    BeanUtils.copyProperties(sysFlyash, sysFlyashVO);
//                    vo.setFlyashList(Collections.singletonList(sysFlyashVO));
//                } else {
//                    vo.setFlyashList(Collections.emptyList());
//                }
//            }*/
//
//            // 设置减水剂数据
//            if (entity.getReducerId() != null) {
//                SysWaterReducer sysWaterReducer = reducerMap.get(entity.getReducerId());
//                if (sysWaterReducer != null) {
//                    AdditiveWaterReducerVO waterReducerVO = new AdditiveWaterReducerVO();
//                    BeanUtils.copyProperties(sysWaterReducer, waterReducerVO);
//                    vos.setWaterReducerList(Collections.singletonList(waterReducerVO));
//                } else {
//                    vos.setWaterReducerList(Collections.emptyList());
//                }
//            }
//
//            // 设置增效剂数据
//            if (entity.getAgentId() != null) {
//                SysEfficiencyAgent sysEfficiencyAgent = efficiencyAgentMap.get(entity.getAgentId());
//                if (sysEfficiencyAgent != null) {
//                    SysEfficiencyAgentVO sysEfficiencyAgentVO = new SysEfficiencyAgentVO();
//                    BeanUtils.copyProperties(sysEfficiencyAgent, sysEfficiencyAgentVO);
//                    vos.setEfficiencyAgentList(Collections.singletonList(sysEfficiencyAgentVO));
//                } else {
//                    vos.setEfficiencyAgentList(Collections.emptyList());
//                }
//            }
//
//            // 设置矿粉数据
//            if (entity.getPowderId() != null) {
//                SysMineralPowder sysMineralPowder = mineralPowderMap.get(entity.getPowderId());
//                if (sysMineralPowder != null) {
//                    SysMineralPowderVO sysMineralPowderVO = new SysMineralPowderVO();
//                    BeanUtils.copyProperties(sysMineralPowder, sysMineralPowderVO);
//                    vos.setMineralList(Collections.singletonList(sysMineralPowderVO));
//                } else {
//                    vos.setMineralList(Collections.emptyList());
//                }
//            }
//
//            // 设置其他数据
//            if (entity.getAdmixturesId() != null) {
//                SysOtherAdmixtures sysOtherAdmixtures = otherAdmixturesMap.get(entity.getAdmixturesId());
//                if (sysOtherAdmixtures != null) {
//                    SysOtherAdmixturesVO sysOtherAdmixturesVO = new SysOtherAdmixturesVO();
//                    BeanUtils.copyProperties(sysOtherAdmixtures, sysOtherAdmixturesVO);
//                    vos.setOtherList(Collections.singletonList(sysOtherAdmixturesVO));
//                } else {
//                    vos.setOtherList(Collections.emptyList());
//                }
//            }
//            //膨胀剂
//            if (entity.getSwellingagentId() != null) {
//                SysSwellingagent sysOtherAdmixtures = swellingagentMap.get(entity.getAdmixturesId());
//                if (sysOtherAdmixtures != null) {
//
//                    SwellingagentVO swellingagentVO = new SwellingagentVO();
//                    BeanUtils.copyProperties(sysOtherAdmixtures, swellingagentVO);
//                    vos.setSwellingagentList(Collections.singletonList(swellingagentVO));
//                } else {
//                    vos.setSwellingagentList(Collections.emptyList());
//                }
//            }
//
//            return vos;
//        }).collect(Collectors.toList());
//
//
//        return results;
//    }








    /**
     * 导出excelText
     * @return
     */
    @Override
    public List<SysDeformationPerformance> exportMistedDeformationPerformanceExcelText(ExportDeformationPerformanceExcelDTO exportDeformationPerformanceExcelDTO) {
        List<Integer> deformationIds = exportDeformationPerformanceExcelDTO.getDeformationIds();

        if (deformationIds == null || deformationIds.isEmpty()) {
            return Collections.emptyList();
        }



        List<SysDeformationPerformance> sysDeformationPerformanceList=   sysDeformationPerformanceMapper.exportMistedDeformationPerformanceExcelText(deformationIds);

        return sysDeformationPerformanceList;
    }

    /**
     * 到处excel测试
     * @param durabilityIds
     * @return
     */
    @Override
    public List<SysDeformationPerformance> exportDurabilityPerformanceExcelText(List<Integer> durabilityIds) {
        List<SysDeformationPerformance> sysDeformationPerformanceList=  sysDeformationPerformanceMapper.exportDurabilityPerformanceExcelText(durabilityIds);
        return sysDeformationPerformanceList;
    }


}
